
package view.client;

import view.client.Console;
import model.Hearer;
import controller.link.game.SellItem;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.DropMode;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.TransferHandler;
import model.game.Item;

public class DialogSeller extends javax.swing.JDialog implements ActionListener, MouseListener {
    private Console csl;
    private PanelGame pg;
    private ArrayList<Item> items;
    private Hearer h;
    private DefaultListModel modelItem = new DefaultListModel();
    public DialogSeller(java.awt.Frame parent, Console csl, PanelGame pg, ArrayList<Item>items, Hearer h) {
        super(parent, false);        
        initComponents();
        this.h = h;
        this.items = items;
        for (int i=0; i<items.size(); i++){
            modelItem.add(i, items.get(i).getName());
        }
        btnCancel.addActionListener(this);
        listItems.addMouseListener(this);
        listItems.setTransferHandler(new DragAndDrop(TransferHandler.MOVE));
        listItems.setPrototypeCellValue("List Item WWWWWW");
        listItems.setDragEnabled(true);
        listItems.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        listItems.setDropMode(DropMode.INSERT);
        this.csl = csl;
        this.pg = pg;
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listItems = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        lblPrice = new javax.swing.JLabel();
        lblTitulo = new javax.swing.JLabel();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        listItems.setModel(modelItem);
        jScrollPane1.setViewportView(listItems);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Preço:");

        lblPrice.setFont(new java.awt.Font("Tahoma", 0, 36));
        lblPrice.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblPrice.setText("-");

        lblTitulo.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblTitulo.setText("Vendedor");

        btnCancel.setText("Cancel");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblPrice, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(154, 154, 154)
                        .addComponent(lblTitulo))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(151, 151, 151)
                        .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(lblTitulo)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(lblPrice))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addComponent(btnCancel)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblPrice;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JList listItems;
    // End of variables declaration//GEN-END:variables

    public javax.swing.JButton getBtnCancel() {
        return btnCancel;
    }

    public javax.swing.JLabel getLblPrice() {
        return lblPrice;
    }

    public javax.swing.JLabel getLblTitulo() {
        return lblTitulo;
    }

    public javax.swing.JList getListItems() {
        return listItems;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        this.dispose();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource().equals(listItems)){
            if ((listItems.isEnabled()) && (listItems.getModel().getSize()>0)){
                         int index = listItems.locationToIndex(e.getPoint());
                         ListModel dlm = listItems.getModel();
                         Object item = dlm.getElementAt(index);
                         listItems.ensureIndexIsVisible(index);
                         if (listItems.getModel().equals(modelItem)){
                             for (int i=0; i<items.size(); i++){
                                 if (item.toString().equals(items.get(i).getName())){
                                     lblPrice.setText(items.get(i).getPrice()+"");
                                     break;
                                 }
                             }
                         }
                }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
    class DragAndDrop extends TransferHandler {
        int action;
         
        public DragAndDrop(int action) {
            this.action = action;
        }
         
        public boolean canImport(TransferHandler.TransferSupport support) {
            // for the demo, we'll only support drops (not clipboard paste)
            if (!support.isDrop()) {
                return false;
            }
 
            // we only import Strings
            if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                return false;
            }
 
            boolean actionSupported = (action & support.getSourceDropActions()) == action;
            if (actionSupported) {
                support.setDropAction(action);
                return true;
            }
 
            return false;
        }
 
        public boolean importData(TransferHandler.TransferSupport support) {
            // if we can't handle the import, say so
            if (!canImport(support)) {
                return false;
            }
            // fetch the drop location
            JList.DropLocation dl = (JList.DropLocation)support.getDropLocation();
 
            int index = dl.getIndex();
            
            // fetch the data and bail if this fails
            String data;
            try {
                data = (String)support.getTransferable().getTransferData(DataFlavor.stringFlavor);
            } catch (UnsupportedFlavorException e) {
                return false;
            } catch (java.io.IOException e) {
                return false;
            }
            if (support.getComponent().equals(listItems)){
                if (data.contains("inventory/")){
                    String data2 = data.substring(10);
                    SellItem si = new SellItem(h, data2);
                    return true;
                }
            }
 
            return true;
        }
        public int getSourceActions(JComponent comp) {
            return COPY_OR_MOVE;
        }
 
        private int index = 0;
 
        public Transferable createTransferable(JComponent comp) {
            JList a = (JList)comp;
            String object = "";
            if (comp.equals(listItems)){
                object = "items/";
                
            }
            index = a.getSelectedIndex();
            if (index < 0 || index >= a.getModel().getSize()) {
                return null;
            }
 
            return new StringSelection(object+(String)a.getSelectedValue());
        }
         
        public void exportDone(JComponent comp, Transferable trans, int action) {
            if (action != MOVE) {
                return;
            }
 
            //modelObject.removeElementAt(index);
        }
    } 
}
